package LeetCode刷题.暑假刷的算法;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-07-19 16:10
 * @description: Functions of this class is 层序遍历二叉树
 **/
public class 层序遍历二叉树 {
    private static class TNode{
        TNode left,right;
        Integer val;
    }
    public static void main(String args[]){
        int nums[]={1,2,3,4,5,6,7,8,9,10};
        TNode T=createTree(nums,0,nums.length-1);
        System.out.print("队列法层序遍历：");
        levelOrder1(T);
        System.out.print("\n二叉树概念法层序遍历：");
        levelOrder2(T);
    }
    private static TNode createTree(int nums[],int left,int right){
        TNode root=null;
        if(left<=right){
            int mid=(left+right+1)>>1;
            root=new TNode();
            root.val=nums[mid];
            root.left=createTree(nums,left,mid-1);
            root.right=createTree(nums,mid+1,right);
        }
        return root;
    }
    private static void levelOrder1(TNode T){
        if(T!=null){
            Queue<TNode> q=new LinkedList<>();
            q.add(T);
            while (!q.isEmpty()){
                TNode p=q.poll();
                System.out.print(p.val+" ");
                if(p.left!=null){
                    q.add(p.left);
                }
                if(p.right!=null){
                    q.add(p.right);
                }
            }
        }
    }
    private static int depthTree(TNode T){
        if(T==null){
            return 0;
        }
        return Math.max(depthTree(T.left),depthTree(T.right))+1;
    }
    private static void levelOrder2(TNode T){
        int level=depthTree(T);
        for(int i=0;i<level;i++){

        }
    }
}